package com.fujimana.rockincat.restree.init.message;

import com.fujimana.rockincat.restree.annotation.message.Messages;
import com.fujimana.rockincat.restree.init.Handler;
import com.fujimana.rockincat.restree.init.Item;
import com.fujimana.rockincat.restree.util.ClassUtils;

public class MessageHandler implements Handler<Item>{

	@Override
	public Item handle(String resource) {
		
		Class<?> clazz = ClassUtils.loadClass(resource);
		
		MessageItem item = new MessageItem();
		
		for(Object o : clazz.getEnumConstants()) {
			
			if(o.getClass().getAnnotation(Messages.class) != null) {
				item.addMessages((Enum<?>) o);
			}
			
		}
		
		return item;
	}

	@Override
	public boolean isHandle(String resource) {
		if (!resource.endsWith(".class")) {
			return false;
		}
		
		Class<?> clazz = ClassUtils.loadClass(resource);
		
		if(clazz == null) {
			return false;
		}
		
		if(clazz.getEnumConstants() == null) {
			return false;
		}

		return clazz.getAnnotation(Messages.class) != null;
	}

}
